package com.looso.sfolder.ui;

import com.looso.sfolder.data.FileData;
import com.looso.sfolder.net.HttpConnectionDemo;
import com.looso.sfolder.security.Base64;
import com.looso.sfolder.security.DES;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.view.View;

public class ImagesetReaderActivity extends Activity implements View.OnClickListener{
	
	private static final boolean TEST_TEST = false; 
	
	//data
	private FileData mData = null;
	
	// view
	private WebView mWebView;
	private TextView pageView; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		
		Bundle bundle = getIntent().getExtras();
		mData = (FileData) bundle.get("data");
		
		initView();
		initData();
	}
	
	private void initView() {
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		// controller
		TableLayout controller = new TableLayout(this);
		controller.setShrinkAllColumns(true);
		TableRow row = new TableRow(this);
		Button firstButton = new Button(this);
		firstButton.setText("第一页");
		firstButton.setOnClickListener(this);
		row.addView(firstButton);
		Button prevButton = new Button(this);
		prevButton.setText("上一页");
		prevButton.setOnClickListener(this);
		row.addView(prevButton);
		Button nextButton = new Button(this);
		nextButton.setText("下一页");
		nextButton.setOnClickListener(this);
		row.addView(nextButton);
		Button lastButton = new Button(this);
		lastButton.setText("最后一页");
		lastButton.setOnClickListener(this);
		row.addView(lastButton);
		pageView = new TextView(this);
		pageView.setText("");
		row.addView(pageView);
		controller.addView(row, new TableLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		layout.addView(controller, new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));

		// webview for page display
		mWebView = new WebView(this);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setBuiltInZoomControls(true);
		webSettings.setSupportZoom(true);
		mWebView.setWebViewClient(new ReaderWebViewClient(this));
		layout.addView(mWebView);

		setContentView(layout);
	}
	
	private class ReaderWebViewClient extends WebViewClient{
		public ReaderWebViewClient(Context context) {
			// TODO Auto-generated constructor stub
		}
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			return super.shouldOverrideUrlLoading(view, url);
		}
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			setProgressBarIndeterminateVisibility(true);
			super.onPageStarted(view, url, favicon);
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			String titleString = view.getTitle();
			super.onPageFinished(view, url);
			getWindow().setTitle(titleString);

			setProgressBarIndeterminateVisibility(false);
		}
		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();			
		}
	}
	
	private void initData() {
		if (mData != null) {
			pageView.setText(mData.getCurrentPageIndex()+"/"+mData.getPageCount());
			if(TEST_TEST){
				loadDataWithoutEncrypt(mData.getCurrentPageUrl());
			}else{
				loadData(mData.getCurrentPageUrl());
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		if(mData==null){
			return ;
		}
		String text = (String) ((Button)v).getText();
		String url = null;
		if(text.equals("第一页")){
			url = mData.goFirstPage();			
		}else if(text.equals("上一页")){			
			url = mData.goPrevPage();			
		}else if(text.equals("下一页")){
			url = mData.goNextPage();			
		}else if(text.equals("最后一页")){
			url = mData.goLastPage();
		}
		if(url!=null){
			setProgressBarIndeterminateVisibility(true);
			pageView.setText(mData.getCurrentPageIndex()+"/"+mData.getPageCount());
			if(TEST_TEST){
				loadDataWithoutEncrypt(url);
			}else{
				loadData(url);
			}			
		}
	}
	
	
	private void loadData(String url){
		if(mWebView!=null && mData!=null){
			String key = mData.getKey();
			byte[] source = HttpConnectionDemo.sendHttpsGet(url);
			byte[] data = DES.decryptByDES(key, source);
			if(data!=null){
//				mWebView.loadData(new String(data), "image/jpeg", "utf-8");				
				String image64 = Base64.encode(data);
		        String mimeType = "text/html";
		        String encoding = null;
		        String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />";	       
		        mWebView.loadDataWithBaseURL(null, pageData, mimeType, encoding, "");
			}
		}
	}
	
	private void loadDataWithoutEncrypt(String url){
//		byte[] source = HttpConnectionDemo.sendHttpsGet(url);
//		if(source!=null){
//			mWebView.loadData(new String(source), "image/jpg", "utf-8");
//		}
		mWebView.loadUrl(url);
	}
}

